import time

from Base import Base
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from ddt import file_data,ddt

class Page(Base):
    login_loc = (By.XPATH,'//a[@href="https://login.jiuxian.com/login.htm"]')
    def login(self):
        self.click(self.login_loc)

    username_loc = (By.NAME,'uname')
    def username(self):
        # user = WebDriverWait(self.driver,20,0.5).until(EC.presence_of_element_located(self.username_loc))
        self.send_keys(self.username_loc,'18620064667')

    password_loc = (By.NAME,'_pwd')
    def password(self):
        # passw = WebDriverWait(self.driver, 20, 0.5).until(EC.presence_of_element_located(self.password_loc))
        self.send_keys(self.password_loc,'Yukaiwen2580')

    loginbtn_loc = (By.ID,'save')
    def loginbtn(self):
        self.click(self.loginbtn_loc)

    que_loc = (By.XPATH,'//a[@id="bindSub" and @href="https://www.jiuxian.com/"]')
    def que(self):
        self.click(self.que_loc)

    dy1_loc = (By.XPATH,'/html/body/div[2]/div/div[1]/div/p/span')
    def dy1(self):
        dy1 = self.text(self.dy1_loc)
        print(dy1)

    dy2_loc = (By.XPATH,'//*[@id="brandAll"]/ul/li[1]/a')
    def dy2(self):
        dy2 = self.text(self.dy2_loc)
        print(dy2)

    dy3_loc = (By.XPATH,'/html/body/div[5]/div[2]/div[2]/div[1]/h1/text()')
    def dy3(self):
        dy3 = self.text(self.dy3_loc)
        print(dy3)

    dy4_loc = (By.XPATH,'/html/body/div[4]/div[2]/div[1]/div/div[2]/p[1]')
    def dy4(self):
        dy4 = self.text(self.dy4_loc)
        print(dy4)

    dy5_loc = (By.XPATH,'/html/body/div[4]/div[1]/em')
    def dy5(self):
        dy5 = self.text(self.dy5_loc)
        print(dy5)

    search_loc = (By.ID,'wd')
    def search(self):
        self.send_keys(self.search_loc,'茅台')

    searchbtn_loc = (By.CLASS_NAME,'search-btn')
    def searchbtn(self):
        self.click(self.searchbtn_loc)

    maotai_loc = (By.XPATH,'/html/body/div[6]/div/div[2]/div[5]/ul/li[1]/div[2]/div[1]/a/img')
    def maotai(self):
        self.click(self.maotai_loc)

    addgwc_loc = (By.ID,'addToCartForDetail')
    def addgwc(self):
        self.click(self.addgwc_loc)

    seegwc_loc = (By.CLASS_NAME,'u-buy-go')
    def seegwc(self):
        self.click(self.seegwc_loc)

    wode_loc = (By.XPATH,'/html/body/div[2]/div/div[2]/ul/li[1]/div[1]/a')
    def wode(self):
        self.click(self.wode_loc)

    guan_loc = (By.XPATH,'//*[@id="uGuideBox"]/div/div/span')
    def guan(self):
        self.click(self.guan_loc)

    address_loc = (By.XPATH,'/html/body/div[4]/div[1]/div[2]/div[4]/a[3]/span')
    def address(self):
        self.click(self.address_loc)

    shouhuo_loc = (By.XPATH,'//input[@id="people" or @name="consignee"]')
    def shouhuo(self):
        self.send_keys(self.shouhuo_loc,'余凯文')

    sf_loc = (By.ID,'province')
    def sf(self):
        sf = self.find_element(self.sf_loc)
        sf.select_by_value('2')

    sq_loc = (By.ID, 'city')
    def sq(self):
        sq = self.find_element(self.sq_loc)
        sq.select_by_value('52')

    cq_loc = (By.ID, 'district')
    def cq(self):
        cq = self.find_element(self.cq_loc)
        cq.select_by_value('512')

    dizhi_loc = (By.NAME,'addressMore')
    def dizhi(self):
        self.send_keys(self.dizhi_loc,'123')

    phone_loc = (By.NAME,'mobile')
    def phone(self):
        self.send_keys(self.phone_loc,'18620064667')

    tijiao_loc = (By.CLASS_NAME,'qjwSubmit2')
    def tijiao(self):
        self.click(self.tijiao_loc)

    ok_loc = (By.XPATH,'/html/body/div[8]/div/div[3]/span[1]/b')
    def ok(self):
        self.click(self.ok_loc)

    def luoji_Jiuxian_login(self):
        self.open_url('https://www.jiuxian.com/')
        self.login()
        self.username()
        self.password()
        time.sleep(10)
        self.loginbtn()
        time.sleep(1)
        self.que()
        time.sleep(4)
        self.dy1()

    def luoji_Jiuxian_search(self):
        self.luoji_Jiuxian_login()
        self.search()
        self.searchbtn()
        time.sleep(1)
        self.dy2()
        self.maotai()
        self.qie()
        self.dy3()
        self.addgwc()
        time.sleep(2)
        self.seegwc()
        time.sleep(2)

    def luoji_Jiuxian_address(self):
        self.luoji_Jiuxian_login()
        try:
            self.wode()
            self.qie()
            time.sleep(2)
            self.guan()
            self.dy4()
            self.driver.implicitly_wait(10)
            self.address()
            self.dy5()
            self.shouhuo()
            time.sleep(1)
            self.sf()
            time.sleep(1)
            self.sq()
            time.sleep(1)
            self.cq()
            time.sleep(1)
            self.dizhi()
            self.phone()
            time.sleep(1)
            self.tijiao()
            time.sleep(1)
            self.ok()
        except Exception as e:
            et = time.strftime('%Y%m%d%H%M',time.localtime())
            print(e)
            self.driver.save_screenshot(et+'.png')